/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.api.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import com.koron.workflow.api.bean.TaskListBean;
import com.koron.workflow.api.mapper.TaskListMapper;
import com.koron.workflow.api.service.ActivitiService;
import com.koron.workflow.api.service.TaskListService;
import com.koron.workflow.baseConfig.mapper.StaffMapper;
import com.koron.workflow.common.bean.StaffBean;
import com.koron.workflow.common.util.BusinessCode;
import com.koron.workflow.common.util.WorkflowUtil;

@Service
public class TaskListServiceImpl implements TaskListService{
	
	@Autowired
	private ActivitiService activitiService;

	/**
	 *
	 * @param factory
	 * @param task
	 * @param title 标题
	 * @return
	 */
	@Override
	public MessageBean<?> addTaskList(SessionFactory factory, Task task, String title) {
		List<HistoricTaskInstance> hisTasks = activitiService.getHistoricTaskByProcessInsId(task.getProcessInstanceId());
		TaskListBean taskList = new TaskListBean();
		taskList.setId(WorkflowUtil.getObjectId());
		taskList.setTitle(title);
		taskList.setCurNodeCode(task.getTaskDefinitionKey());
		taskList.setCurNodeName(task.getName());
		taskList.setCurTaskId(task.getId());
		taskList.setProcessInstanceId(task.getProcessInstanceId());
		if(hisTasks != null && hisTasks.size() > 0) {
			HistoricTaskInstance hisTask = hisTasks.get(0);
			taskList.setPreOperationTime(hisTask.getEndTime());
		}
		List<String> userCodes = activitiService.getCandidateUsers(task.getId());
		List<StaffBean> staffs = WorkflowUtil.getStaffByAccount(userCodes);
		if(userCodes != null  && userCodes.size() > 0) {
			taskList.setCurOpertor(StringUtils.join(userCodes, ","));
			taskList.setCurOpertorName(staffs.stream().filter(t -> t != null).map(t -> t.getName()).collect(Collectors.joining(",")));
		}else {
			taskList.setCurOpertor("system_operation");
			taskList.setCurOpertorName("系统操作员");
		}
		taskList.setStatus(1);
		factory.getMapper(TaskListMapper.class).addTaskList(taskList);
		return MessageBean.create(BusinessCode.SUCCESS.getCode(), "添加任务列表成功", Void.class);
	}

	/**
	 * 修改任务列表
	 * @param factory
	 * @param taskId
	 */
	@Override
	public MessageBean<?> updateTaskList(SessionFactory factory, String taskId, String assignee) {
		factory.getMapper(TaskListMapper.class).updateStatusAndAssigee(taskId, 2, assignee);
		return MessageBean.create(BusinessCode.SUCCESS.getCode(), "修改成功", Void.class);
	}

	/**
	 * 修改任务处理人
	 */
	@Override
	public MessageBean<?> updateTaskListOperator(SessionFactory factory, String taskId, String operator) {
		List<String> operators = new ArrayList<String>();
		if(operator.contains(",")) {
			operators = Arrays.asList(operator.split(","));
		}else {
			operators.add(operator);
		}
		Stream<StaffBean> staffs = operators.stream().map(t -> factory.getMapper(StaffMapper.class).queryByAccount(t));
		String operatorName = staffs.filter(t -> t != null).map(t -> t.getName()).collect(Collectors.joining(","));
		factory.getMapper(TaskListMapper.class).updateOpertor(taskId, operator, operatorName);
		return MessageBean.create(BusinessCode.SUCCESS.getCode(), "修改成功", Void.class);
	}

	@Override
	public MessageBean<?> addTaskList(SessionFactory factory, Task task, String title, int status) {
		List<HistoricTaskInstance> hisTasks = activitiService.getHistoricTaskByProcessInsId(task.getProcessInstanceId());
		TaskListBean taskList = new TaskListBean();
		taskList.setId(WorkflowUtil.getObjectId());
		taskList.setTitle(title);
		taskList.setCurNodeCode(task.getTaskDefinitionKey());
		taskList.setCurNodeName(task.getName());
		taskList.setCurTaskId(task.getId());
		taskList.setProcessInstanceId(task.getProcessInstanceId());
		if(hisTasks != null && hisTasks.size() > 0) {
			HistoricTaskInstance hisTask = hisTasks.get(0);
			taskList.setPreOperationTime(hisTask.getEndTime());
		}
		List<String> userCodes = activitiService.getCandidateUsers(task.getId());
		List<StaffBean> staffs = WorkflowUtil.getStaffByAccount(userCodes);
		if(userCodes != null  && userCodes.size() > 0) {
			taskList.setCurOpertor(StringUtils.join(userCodes, ","));
			taskList.setCurOpertorName(staffs.stream().filter(t -> t != null).map(t -> t.getName()).collect(Collectors.joining(",")));
		}else {
			taskList.setCurOpertor("system_operation");
			taskList.setCurOpertorName("系统操作员");
		}
		taskList.setStatus(status);
		factory.getMapper(TaskListMapper.class).addTaskList(taskList);
		return MessageBean.create(BusinessCode.SUCCESS.getCode(), "添加任务列表成功", Void.class);
	}

}
